## functions used in creating figures
makeDF <- function(table_name, model_names, fill_table){
  for(i in 1:length(table_name)){
    tmp <- table_name[[i]]
    if("ltotal_cost" %in% names(tmp@coef)){
      index <- "ltotal_cost"
    }else if("sumallLESOaidBG_log" %in% names(tmp@coef)){
      index <- "sumallLESOaidBG_log"
    }else if("milex_iv" %in% names(tmp@coef)){
      index <- "milex_iv"
    }else if("`ltotal_cost(fit)`" %in% names(tmp@coef)){
      index <- "`ltotal_cost(fit)`"
    }else if("`sumallLESOaidBG_log(fit)`" %in% names(tmp@coef)){
      index <- "`sumallLESOaidBG_log(fit)`"
    }else if("`log(1 + sumallLESOaidBG)(fit)`" %in% names(tmp@coef)){
      index <- "`log(1 + sumallLESOaidBG)(fit)`"
    }else if("log(1 + sumallLESOaidBG)" %in% names(tmp@coef)){
      index <- "log(1 + sumallLESOaidBG)"
    }
    coef <- tmp@coef[index]
    se <- tmp@se[index]
    fill_table[i,] <- c(coef,se,model_names[i],who_names[i])
    
  }
  fill_table <- as.data.frame(fill_table)
  names(fill_table) <- c("coef", "se", "model", "group")
  return(fill_table)
}

makeDFharris <- function(table_name, model_names, fill_table,outcome_type){
  for(i in 1:length(table_name)){
    tmp <- table_name[[i]]
    if("`log_sum_items_lag(fit)`" %in% row.names(tmp$coefficients)){
      index <- "`log_sum_items_lag(fit)`"
    }else if("`log(1 + sumallLESOquantityHARRIS)(fit)`" %in% row.names(tmp$coefficients)){
      index <- "`log(1 + sumallLESOquantityHARRIS)(fit)`"
    }else if("`log_sum_value_ttl_lag(fit)`" %in% row.names(tmp$coefficients)){
      index <- "`log_sum_value_ttl_lag(fit)`"
    }else if("`log(1 + sumallLESOaidHARRIS)(fit)`" %in% row.names(tmp$coefficients)){
      index <- "`log(1 + sumallLESOaidHARRIS)(fit)`"
    }
    coef <- tmp$coefficients[index,]
    se <- tmp$se[index]
    fill_table[i,] <- c(coef,se,model_names[i],who_names[i],outcome_type)
    
  }
  fill_table <- as.data.frame(fill_table)
  names(fill_table) <- c("coef", "se", "model", "group", "outcome_type")
  return(fill_table)
}

plot_individual_facets_labels <- function(data, m){
  tmp <- data %>% filter(model == m)
  h <- unique(tmp$lim_height)
  ggplot(data = tmp, aes(x = nice_names, y = estimate, color = color_var)) +
    geom_hline(yintercept = 0, color = "red",linetype="dashed") +
    geom_point()+
    geom_segment(aes(x=nice_names, xend=nice_names, y= low95, yend=up95),size=1) +
    geom_segment(aes(x=nice_names, xend=nice_names, y= low99, yend=up99)) +
    facet_grid(model ~ county,
               labeller = label_wrap_gen(width=10),
               scales='free_x',
               space='free_x',
               switch='y',
               drop = T) +
    scale_y_continuous(limits = c(-h,h),position='right') +
    scale_color_manual(values=mycolors,guide='none') +
    theme_bw() +
    theme(legend.position = "none",
          strip.background =element_rect(fill="azure3"),
          strip.text.y = element_text(size=8,angle=180),
          strip.text.x = element_blank(),
          axis.title.x=element_blank(),
          axis.title.y=element_blank(),
          axis.text.x = element_text(size=8),
          panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_blank(),
          panel.grid.minor.x = element_blank(),
          panel.grid.minor.y = element_blank(),
          plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm"))
}

plot_individual_facets_no_labels <- function(data, m){
  tmp <- data %>% filter(model == m)
  h <- unique(tmp$lim_height)
  ggplot(data = tmp, aes(x = nice_names, y = estimate, color = color_var)) +
    geom_hline(yintercept = 0, color = "red",linetype="dashed") +
    geom_point()+
    geom_segment(aes(x=nice_names, xend=nice_names, y= low95, yend=up95),size=1) +
    geom_segment(aes(x=nice_names, xend=nice_names, y= low99, yend=up99)) +
    facet_grid(model ~ county,
               labeller = label_wrap_gen(width=10),
               scales='free_x',
               space='free_x',
               switch='y',
               drop = T) +
    scale_y_continuous(limits = c(-h,h),position='right') +
    scale_color_manual(values=mycolors,guide='none') +
    theme_bw() +
    theme(legend.position = "none",
          strip.background =element_rect(fill="azure3"),
          strip.text.y = element_text(size=8,angle=180),
          strip.text.x = element_blank(),
          axis.title.x=element_blank(),
          axis.title.y=element_blank(),
          axis.text.x = element_blank(),
          panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_blank(),
          panel.grid.minor.x = element_blank(),
          panel.grid.minor.y = element_blank(),
          axis.ticks = element_blank(),
          plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm"))
}

